//
//  StatusOriginalView.swift
//  SHWeibo2.0
//
//  Created by 黄礼潮 on 16/2/28.
//  Copyright © 2016年 黄礼潮. All rights reserved.
//

import UIKit
import SnapKit

 private let imageSize = CGSize(width: 36, height: 36)

private let margin:CGFloat = 12.0
//原创微博
class StatusOriginalView: UIView {
    //微博视图模型
    var viewModel: StatusViewModel?{
    
       didSet{
      
    
        
        iconView.cz_setImage(viewModel?.status.user?.profile_image_url, CZPlaceholderImage:"avatar_default_big", size: imageSize, isCorner:true)
        nameLabel.text = viewModel?.status.user?.screen_name
        memberIconView.image = viewModel?.usermenberView
        vipImageView.image = viewModel?.userVipView
        
        sourceLabel.text = viewModel?.souceText
        
        timeLabel.text =  viewModel?.dateTime?.cz_dateDescription
        
        contentLabel.text = viewModel?.status.text
        
        //更新约束
        //如果有配图的话，那么更新它的约束，底部一定参照contentLabel
        //没有的话就offset就设置为0 那么没有配图的文字内容距离原创微博的底部有个margin 如果不这么设置可能有两个margin
        let offset = viewModel?.status.pic_urls?.count > 0 ? margin : 0
        pictureView.snp_updateConstraints { (make) -> Void in
           
            make.top.equalTo(contentLabel.snp_bottom).offset(offset)
            make.size.equalTo(viewModel!.pictureSize)
           
          }
        //设置配图视图内容
        pictureView.pic_urls = viewModel?.status.pic_urls
   
       }
    
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupOriginalUI()
       
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //记载控件
    //图像
    private let iconView = UIImageView(image: UIImage(named:"avatar_default_big"))
    //用户名
    private let nameLabel = UILabel(cztitle:"阿潮是个小婊砸")
    //认证图标
    private let memberIconView = UIImageView(image: UIImage(named:"avatar_vip"))
    //Vip
    private let vipImageView = UIImageView(image:UIImage(named:"common_icon_membership"))
    //时间
    private let timeLabel = UILabel(cztitle:"刚刚", fontSize: 10)
    //来源
    private let sourceLabel = UILabel(cztitle:"新浪微博", fontSize:10)
    //正文
    private let contentLabel = UILabel(cztitle:"哈哈")
    //配图
    private let pictureView:StatusPictuerView = StatusPictuerView()
}


extension StatusOriginalView{

    private func setupOriginalUI(){
       
     //   backgroundColor = UIColor.whiteColor()
        //添加控件
        addSubview(iconView)
        addSubview(nameLabel)
        addSubview(vipImageView)
        addSubview(memberIconView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        addSubview(pictureView)
        
        //测试代码
        
        let count = random() % 20 + 1
        var str = ""
        for _ in 0..<count {
        
           str += "你个小婊砸"
        }
        contentLabel.text = str
        
        UIImage(named:"avatar_default_big")?.cz_drawImage(imageSize, isCorner: true, finished: { (image) -> () in
            self.iconView.image = image
        })
  //自动布局
        iconView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self).offset(margin)
            make.left.equalTo(self).offset(margin)
            make.size.equalTo(imageSize)
        }
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(iconView.snp_right).offset(margin)
            make.top.equalTo(iconView)
        }
        memberIconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_right)
            make.centerY.equalTo(iconView.snp_bottom)
        }
        vipImageView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(nameLabel)
            make.left.equalTo(nameLabel.snp_right).offset(margin)
        }
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(iconView)
            make.left.equalTo(iconView.snp_right).offset(margin)
        }
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(timeLabel.snp_right).offset(margin)
            make.bottom.equalTo(timeLabel)
        }
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView.snp_bottom).offset(margin)
            make.left.equalTo(iconView)
            make.right.equalTo(self).offset(-margin)
            //一定要设置底部的约束，才会自动计算行高   很重要了，搞了一晚的Bug
        //    make.bottom.equalTo(self).offset(-margin)
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(contentLabel)
            make.top.equalTo(contentLabel.snp_bottom).offset(margin)
            // 调试用的
            make.size.equalTo(CGSizeZero)
            make.bottom.equalTo(self).offset(-margin)
        }
        
    }

}